/**
 *
 */
package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.teng.smartdrawer.R;
import com.teng.smartdrawer.bean.PopWindowMenu;

import java.util.ArrayList;

public class MenuAdapter extends BaseAdapter {
    private Context mContext;

    public MenuAdapter(Context mcon, ArrayList<PopWindowMenu> mMenus) {
        mContext = mcon;
        mValues = mMenus;
    }

    private ArrayList<PopWindowMenu> mValues = new ArrayList<PopWindowMenu>();


    @Override
    public int getCount() {
        if (mValues != null && mValues.size() > 0) {
            return mValues.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        if (mValues != null && mValues.size() > 0) {
            return mValues.get(arg0);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (mValues != null && mValues.size() > 0) {
            return position;
        }
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (mValues == null || mValues.size() == 0) {
            return null;
        }
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.menu_listview_item, null);
            holder.image = (ImageView) convertView.findViewById(R.id.menu_listiVew_imageView);
            holder.title = (TextView) convertView.findViewById(R.id.menu_listView_textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final PopWindowMenu info = mValues.get(position);
        holder.title.setText(info.getTextContent());
        if (info.getResourceNameId() != 0) {
            holder.image.setVisibility(View.VISIBLE);
            holder.image.setBackgroundResource(info.getResourceNameId());
        } else {
            holder.image.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        public TextView title;
        public ImageView image;
    }
}
